package com.heima.alarm.controller;

import com.heima.alarm.config.AlarmEmailProperties;
import com.heima.alarm.model.AlarmDTO;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/alarm")
@Log4j2
public class AlarmController {

    @Autowired
    JavaMailSender javaMailSender;
    @Autowired
    AlarmEmailProperties alarmEmailProperties;


    @PostMapping("emailNotify")
    public void emailAlarm(@RequestBody List<AlarmDTO> alarmDTOS) {

        // 发送告警邮件/短信
        //1 收件人
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        String[] strings = alarmEmailProperties
                .getReceiveEmails().toArray(new String[]{});
        mailMessage.setTo(strings);

        //2 主题
        mailMessage.setSubject("SkyWalking 告警邮件");
        //3 内容
        mailMessage.setText(alarmDTOS.toString());
        //4 发件人
        mailMessage.setFrom(alarmEmailProperties.getFrom());

        javaMailSender.send(mailMessage);

        log.info("SkyWalking发送告警邮件成功......");

    }


}

